# 登录页面
import time

from selenium.webdriver.common.by import By

from base.base_page import BasePage, BaseHandle
from utils import UtilsDriver


class LoginPage(BasePage):  # 对象层
    def __init__(self):
        super().__init__()
        # self.driver = UtilsDriver.get_driver()
        # 用户名输入框
        self.username = By.ID, "username"
        # 密码输入框
        self.password = By.ID, "password"
        # 验证码输入框
        self.code = By.ID, "verify_code"
        # 登录按钮
        self.login_btn = By.CLASS_NAME, "J-login-submit"

    # 用户名输入框
    def get_username(self):
        # return self.driver.find_element(*self.username)
        return self.get_element(self.username)

    # 密码输入框
    def get_password(self):
        # return self.driver.find_element(*self.password)
        return self.get_element(self.password)

    # 验证码输入框
    def get_code(self):
        # return self.driver.find_element(*self.code)
        return self.get_element(self.code)

    # 登录按钮
    def get_login_btn(self):
        # return self.driver.find_element(*self.login_btn)
        return self.get_element(self.login_btn)


# 操作层
class LoginHandle(BaseHandle):
    def __init__(self):
        self.login_page = LoginPage()  # 实例化对象库层

    # 输入用户名
    def input_username(self, username):
        # self.login_page.get_username().clear()  # 清除输入框中内容
        # self.login_page.get_username().send_keys(username)
        self.input_text(self.login_page.get_username(),username)

    # 输入密码
    def input_password(self, password):
        # self.login_page.get_password().clear()
        # self.login_page.get_password().send_keys(password)
        self.input_text(self.login_page.get_password(), password)

    # 输入验证码
    def input_code(self, code):
        # self.login_page.get_code().clear()
        # self.login_page.get_code().send_keys(code)
        self.input_text(self.login_page.get_code(), code)

    # 点击登录按钮
    def click_login_btn(self):
        self.login_page.get_login_btn().click()


# 业务层
class LoginProxy:
    def __init__(self):
        self.login_handle = LoginHandle()  # 实例化操作层

    # 业务功能
    # 完成登录的业务功能
    def login(self, username, password, code):
        self.login_handle.input_username(username)  # 输入用户名
        self.login_handle.input_password(password)  # 输入密码
        self.login_handle.input_code(code)  # 输入验证码
        self.login_handle.click_login_btn()  # 点击登录按钮
        time.sleep(1)
